package com.bishe.psy.controller;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.config.AppConfig;
import com.bishe.psy.model.AdmUser;
import com.bishe.psy.model.ConUser;
import com.bishe.psy.model.Message;
import com.bishe.psy.model.StuUser;
import com.bishe.psy.service.IAdmUserService;
import com.bishe.psy.service.IConUserService;
import com.bishe.psy.service.IMessageService;
import com.bishe.psy.service.IStuUserService;
import com.bishe.psy.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-30
 * Time: 13:03
 */

@Slf4j
@Api(tags = "站内信测试接口")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService iMessageService;

    @Resource
    private IStuUserService iStuUserService;

    @Resource
    private IConUserService iConUserService;

    @Resource
    private IAdmUserService iAdmUserService;



    @ApiOperation("发送站内信")
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @ApiParam(value = "接收者Id") @RequestParam("receiveUserId") Long receiveUserId,
                          @ApiParam(value = "发送内容") @RequestParam("content")String content,
                          @ApiParam(value = "发送者身份") @RequestParam("currentIdentity")Long currentIdentity) {

        //1.参数校验
        if(receiveUserId == null || currentIdentity == null || StringUtil.isEmpty(content)) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //2.获取用户信息
        Message message = new Message();
        HttpSession session = request.getSession(false);

        if(currentIdentity == 1) {
            StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

            //1.不能给自己发站内信
            if(stuUser.getId() == receiveUserId) {
                return AppResult.failed("不能给自己发送站内信");
            }
            //2.查询发送用户是否存在
            StuUser selectStuUser = iStuUserService.selectByPrimaryKey(stuUser.getId());
            if(selectStuUser.getDeleteState() == 1) {
                return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
            }
            //3.封装message对象的postUserId
            message.setPostUserId(stuUser.getId());

        } else if (currentIdentity == 2) {
            ConUser conUser = (ConUser) session.getAttribute(AppConfig.USER_SESSION);
            //1.不能给自己发站内信
            if(conUser.getId() == receiveUserId) {
                return AppResult.failed("不能给自己发送站内信");
            }
            //2.查询发送用户是否存在
            ConUser selectConUser = iConUserService.selectByPrimaryKey(conUser.getId());
            if(selectConUser.getDeleteState() == 1) {
                return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
            }
            //3.封装message对象的postUserId
            message.setPostUserId(conUser.getId());

        } else {
            AdmUser admUser = (AdmUser) session.getAttribute(AppConfig.USER_SESSION);
            //1.不能给自己发站内信
            if(admUser.getId() == receiveUserId) {
                return AppResult.failed("不能给自己发送站内信");
            }
            AdmUser selectAdmUser = iAdmUserService.selectByPrimaryKey(admUser.getId());
            if(selectAdmUser.getDeleteState() == 1) {
                return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
            }
            //3.封装message对象的postUserId
            message.setPostUserId(admUser.getId());
        }

        //3.封装对象
        message.setReceiveUserId(receiveUserId);
        message.setCurrentIdentity(currentIdentity);
        message.setContent(content);

        //4.调用service
        iMessageService.create(message);

        //5.返回结果
        return AppResult.success();
    }


    @ApiOperation("查询未读站内信")
    @GetMapping("/getConUnreadCount")
    public AppResult<Integer> getConUnreadCount(HttpServletRequest request,
                                                @ApiParam(value = "用户身份") @RequestParam("currentIdentity") Long currentIdentity) {

        //1.获取用户信息
        System.out.println(currentIdentity);
        Integer count = 0;
        HttpSession session = request.getSession(false);
        if(currentIdentity == 1) {
            StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);
            count = iMessageService.selectUnreadCount(stuUser.getId(),2l);

        } else if (currentIdentity == 2) {
            System.out.println("------------------------");
            ConUser conUser = (ConUser) session.getAttribute(AppConfig.USER_SESSION);
            //2.调用service
            count = iMessageService.selectUnreadCount(conUser.getId(),1l);
        }
        //
        return AppResult.success(count);
    }



    @ApiOperation("查询接收者所有的站内信")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request,
                                           @ApiParam(value = "用户身份") @RequestParam("currentIdentity") Long currentIdentity) {

        //1.获取用户信息，获取id
        HttpSession session = request.getSession(false);
        List<Message> messages = null;
        if(currentIdentity == 1) {
            StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);
            //2.调用service
            messages = iMessageService.selectByReceiveUserIdStu(stuUser.getId());
        } else if(currentIdentity == 2) {
            ConUser conUser = (ConUser) session.getAttribute(AppConfig.USER_SESSION);
            //2.调用service
            messages = iMessageService.selectByReceiveUserId(conUser.getId());
        }
        return AppResult.success(messages);
    }



    @ApiOperation("设置站内信为已读状态")
    @PostMapping("/markRead")
    public AppResult markRead(@ApiParam(value = "站内信id")  @RequestParam("id") Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //2.根据id查询站内信是否存在
        Message message = iMessageService.selectByPrimaryKey(id);
        if(message == null || message.getDeleteState() == 1) {
            return AppResult.failed("站内信不存在");
        }

        //3.调用service
        iMessageService.updateStateById(id, (byte) 1);

        //4.返回结果
        return AppResult.success();
    }


    @ApiOperation("回复站内信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @ApiParam("要回复的站内信Id")  @RequestParam("replyId") Long replyId,
                           @ApiParam("回复内容")  @RequestParam("content") String content,
                           @ApiParam("用户身份")  @RequestParam("currentIdentity")Long currentIdentity) {

        //1.非空校验
        if(replyId == null || replyId <= 0 || StringUtil.isEmpty(content)) {
            return AppResult.failed(ResultCode.FAILED);
        }

        //2.获取站内信信息
        Message updateMessage = new Message();

        Message message = iMessageService.selectByPrimaryKey(replyId);
        if(message == null || message.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //3.获取登录用户信息
        HttpSession session = request.getSession(false);
        if(currentIdentity == 1) {
           StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);
           updateMessage.setPostUserId(stuUser.getId());

        } else if(currentIdentity == 2) {
            ConUser conUser = (ConUser) session.getAttribute(AppConfig.USER_SESSION);
            updateMessage.setPostUserId(conUser.getId());
        }

       //4.封装对象
        updateMessage.setReceiveUserId(message.getPostUserId());
        updateMessage.setContent(content);
        updateMessage.setCurrentIdentity(currentIdentity);

        //5.调用service
        iMessageService.reply(replyId,updateMessage);

        //6.返回结果
        return AppResult.success();

    }
}
